<?php
namespace MyAdmin\Controller;

use Think\Controller;

class StatisticsController extends CommonController
{

  public function __construct()
  {
    parent::__construct();
    $this->check_login();
  }

  public function index()
  {
    $page = I("page", 1);
    $page_size = I("page_size", 10);
    //查询

    $shop_name = trim(I("shop_name"));
    $beg_time = trim(I("beg_time"));
    $end_time = trim(I("end_time"));
    $status = trim(I("status"));
    $flag = trim(I("flag"));

    if($status==2){
      $where = '(o.status=2 or o.status=4 or o.status=5) and o.pay_mode!=0';
      $this->assign('status',2);
    }else if($status==7){
      $where = 'o.pay_mode!=0 and o.status = 7';
      $this->assign("status",7);
    }else{
      $where = '(o.status=2 or o.status=4 or o.status=5 or o.status=7) and o.pay_mode!=0';
    }
   
    if ($shop_name) {  //店铺名
      $where .= " and s.name like '%".trim($shop_name)."%'";
      $where_ar['shop_name']=$shop_name;
      $this->assign("shop_name", $shop_name);
    }
    if ($beg_time) {  //下单时间 
      $beg_time_where = strtotime($beg_time);
      $where .= " and o.created_at>='".trim($beg_time_where)."'";
      $where_ar['beg_time']=$beg_time;
      $this->assign("beg_time", $beg_time);
    }
    if ($end_time) {  //下单时间
      $end_time_where = strtotime($end_time);
      $where .= " and o.created_at<'".trim($end_time_where)."'";
      $where_ar['end_time']=$end_time;
      $this->assign("end_time", $end_time);
    }

    $field = 'p.price,p.product_num,p.created_at,o.id,o.shop_id,c.product_id,s.name as shop_name,o.pay_mode,p.name as pname,o.status,sum(o.total_amount) as amount,sum(c.product_num) as num';
    $model = M('Orders')->field($field)->alias('o')->where($where)
    ->join("left join ".C("DB_PREFIX")."order_children c on o.id=c.order_id")
    ->join("left join ".C("DB_PREFIX")."shops s on s.id=o.shop_id")
    ->join("left join ".C("DB_PREFIX")."products p on p.id=c.product_id")
    ->group('c.product_id')
    ->order('num desc');

    if($flag){
      $flag_list = $model->select();
      flagGoods($flag_list);
    }


    $list = $model->page($page.",".$page_size)->select();


    $num =M('Orders')->field('o.id')->alias('o')->where($where)
    ->join("left join ".C("DB_PREFIX")."order_children c on o.id=c.order_id")
    ->join("left join ".C("DB_PREFIX")."shops s on s.id=o.shop_id")
    ->group('c.product_id')->select();
    $num = count($num);

    $page_list = get_html_page_list($url_name, $list, $page, $page_size, $num, $where_ar);

    $this->assign("page_list", $page_list);
    $this->display(); 
  }



  /*
    店铺销量统计
   */
  public function sales_count(){
    $page = I("page", 1);
    $page_size = I("page_size", 10);
    //查询

    $shop_name = trim(I("shop_name"));

    $beg_time = trim(I("beg_time"));
    $end_time = trim(I("end_time"));
    $status = trim(I("status"));

    $flag = trim(I("flag"));
    

    if($status==2){
      $where = '(o.status=2 or o.status=4 or o.status=5) and o.pay_mode!=0';
      $this->assign('status',2);
    }else if($status==7){
      $where = 'o.pay_mode!=0 and o.status = 7';
      $this->assign("status",7);
    }else{
      $where = '(o.status=2 or o.status=4 or o.status=5 or o.status=7) and o.pay_mode!=0';
    }
   
    if ($shop_name) {//订单号
      $where .= " and s.name like '%".trim($shop_name)."%'";
      $where_ar['shop_name']=$shop_name;
      $this->assign("shop_name", $shop_name);
    }
    if ($beg_time) {//下单时间 
      $beg_time_where = strtotime($beg_time);
      $where .= " and o.created_at>='".trim($beg_time_where)."'";
      $where_ar['beg_time']=$beg_time;
      $this->assign("beg_time", $beg_time);
    }
    if ($end_time) {//下单时间
      $end_time_where = strtotime($end_time);
      $where .= " and o.created_at<'".trim($end_time_where)."'";
      $where_ar['end_time']=$end_time;
      $this->assign("end_time", $end_time);
    }


    $field = 's.id,o.shop_id,c.product_num,s.name as shop_name,o.pay_mode,s.status,s.created_at,sum(o.total_amount) as amount,sum(c.product_num) as num';
    $model = M('Orders')->field($field)->alias('o')->where($where)
    ->join("left join ".C("DB_PREFIX")."order_children c on o.id=c.order_id")
    ->join("left join ".C("DB_PREFIX")."shops s on s.id=o.shop_id")
    ->group('s.id')
    ->order('num desc');

    if($flag){
      $flag_list = $model->select();
      flagSales($flag_list);
    }

    $list = $model->page($page.",".$page_size)->select();

    $num =M('Orders')->field('o.id')->alias('o')->where($where)
    ->join("left join ".C("DB_PREFIX")."order_children c on o.id=c.order_id")
    ->join("left join ".C("DB_PREFIX")."shops s on s.id=o.shop_id")
    ->group('o.shop_id')->select();
    $num = count($num);

    $page_list = get_html_page_list($url_name, $list, $page, $page_size, $num, $where_ar);

    $this->assign("page_list", $page_list);

    $this->display(); 
  }
  /*
    店铺提现统计
   */
  public function withdraw_count(){
    $page = I("page", 1);
    $page_size = I("page_size", 10);
    //查询

    $shop_name = trim(I("shop_name"));

    $beg_time = trim(I("beg_time"));
    $end_time = trim(I("end_time"));
    $status = trim(I("status"));

    $flag = trim(I("flag"));

    $where = 'a.status in (2,3)';
    if ($shop_name) {//订单号
      $where .= " and s.name like '%".trim($shop_name)."%'";
      $where_ar['shop_name']=$shop_name;
      $this->assign("shop_name", $shop_name);
    }
    if ($beg_time) {//下单时间 
      $beg_time_where = strtotime($beg_time);
      $where .= " and a.created_at>='".trim($beg_time_where)."'";
      $where_ar['beg_time']=$beg_time;
      $this->assign("beg_time", $beg_time);
    }
    if ($end_time) {//下单时间
      $end_time_where = strtotime($end_time);
      $where .= " and a.created_at<'".trim($end_time_where)."'";
      $where_ar['end_time']=$end_time;
      $this->assign("end_time", $end_time);
    }

    $field = "sum(a.total_amount) as total_amount,s.name as shop_name,s.id,s.status,sum(a.amount) as amount_num,s.amount,s.brokerage,s.created_at";
    $model = M('amount_logs')->field($field)->where($where)
            ->alias('a')
            ->join("left join ".C("DB_PREFIX")."shops s on a.shop_id=s.id")
            ->group('s.id')->order('total_amount desc');

    if($flag){
      $flag_list = $model->select();
      flagWithdraw($flag_list);
    }

    $list = $model->page($page.",".$page_size)->select();


    $num =M('amount_logs')->alias('a')->field('a.id')->where($where)->join("left join ".C("DB_PREFIX")."shops s on a.shop_id=s.id")->group('s.id')->order('total_amount desc')->select();
    $num = count($num);

    $page_list = get_html_page_list($url_name, $list, $page, $page_size, $num, $where_ar);

    $this->assign("page_list", $page_list);

    $this->display(); 
  }


}
